* Analysis with municipal polls
use data_polls.dta, clear
	
global cluster id_city
global size_column "{p{5.5cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1.5cm}p{1.5cm}c}"
global format_table "vce(cluster $cluster) wide(mean1 sd1 mean2 sd2 diff pval) pvalues varlabels  format(%9.2f) nostars nonumbers noobservations nolines"

	global candidates_vars share_male share_married av_age av_education
		label var share_male "Male (\%)"
		label var share_married "Married (\%)"
		label var av_age "Average Age"
		label var av_education "Average Education"
		
	global electoral_vars number_candidates electorate_eff turnout
		label var number_candidates "Number of Candidates"
		label var electorate_eff "Electorate (1,000 people)"
		label var turnout "Turnout"	

	global muni_controls lpib_pc gini illiteracy share_urb radio_am 
		label var lpib_pc "GDP per capita (in ln)"
		label var radio_am "AM Radio Station"
		label var gini "Gini index"
		label var illiteracy "Illiteracy rate"
		label var share_urb	"Share urban population"		
	
* table c.11
preserve
bysort poll_muni: sum electorate_eff ,d	
capture drop comparable
gen comparable = 0
	replace comparable = 1 if electorate_eff<= 250
	replace radio_am = 100 if radio_am == 1	

tab comparable poll_muni		
bysort poll_muni: sum electorate_eff if comparable == 1,d	

balancetable poll_muni $candidates_vars using "tables and figures\table_c11a.tex" if comparable == 1, $format_table replace prehead("\begin{tabularx}{\linewidth}  $size_column {\textit{A. Candidates' characteristics}} \\") postfoot("\end{tabularx}") 
	
balancetable poll_muni $electoral_vars using "tables and figures\table_c11b.tex" if comparable == 1, $format_table replace prehead("\begin{tabularx}{\linewidth}  $size_column {\textit{B. Electoral characteristics}} \\") postfoot("\end{tabularx}") 

balancetable poll_muni $muni_controls using "tables and figures\table_c11c.tex" if comparable == 1, $format_table replace prehead("\begin{tabularx}{\linewidth} $size_column {\textit{C. Municipal characteristics}} \\") postfoot("\end{tabularx}") 	
	
* table c.12
gen comparable2 = 1
	replace comparable2 = 0 if poll_muni == 1 & comparable == 0
	replace comparable2 = 0 if poll_muni == 0 & electorate_eff <= 30
	tab comparable2 poll_muni
	replace radio_am = 100 if radio_am == 1	
	
balancetable poll_muni $candidates_vars using "tables and figures\table_c12a.tex" if comparable2 == 1, $format_table replace prehead("\begin{tabularx}{\linewidth}  $size_column {\textit{A. Candidates' characteristics}} \\") postfoot("\end{tabularx}") 
	
balancetable poll_muni $electoral_vars using "tables and figures\table_c12b.tex" if comparable2 == 1, $format_table replace prehead("\begin{tabularx}{\linewidth}  $size_column {\textit{B. Electoral characteristics}} \\") postfoot("\end{tabularx}") 

balancetable poll_muni $muni_controls using "tables and figures\table_c12c.tex" if comparable2 == 1, $format_table replace prehead("\begin{tabularx}{\linewidth} $size_column {\textit{C. Municipal characteristics}} \\") postfoot("\end{tabularx}") 	
restore
	
* Figure C.2 
keep if poll_muni== 1

gen share_vote_margin100 = share_vote_margin *100
twoway (scatter  poll_begin_mar share_vote_margin100) (lfit  poll_begin_mar share_vote_margin100), graphregion(color(white)) legend( off) xtitle("Previous win margin") ytitle("Expected win margin")
graph export "tables and figures\figure_c2.pdf", replace

eststo clear	